package ciyuan.Item.Water;

import ciyuan.Constants.PlayerDataType;
import ciyuan.DataManager.PlayerDataManager;
import ciyuan.Effect.Effects;
import ciyuan.Item.ItemGroups;
import ciyuan.Item.TypeInterface.LiveType;
import net.minecraft.block.BlockState;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.item.Food;
import net.minecraft.item.ItemStack;
import net.minecraft.item.UseAction;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.potion.EffectInstance;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Direction;
import net.minecraft.util.DrinkHelper;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.util.math.RayTraceContext;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.world.IWorldReader;
import net.minecraft.world.World;
import net.minecraft.world.biome.Biome;
import net.minecraft.world.server.ServerWorld;

/**
 * @author 150149
 */
public class Kettle extends LiveType {

    private static final String WATER_TYPE = "watertype";
    private static final int WATERTYPE_NONE=0;
    private static final int WATERTYPE_FRESHWATER=1;
    private static final int WATERTYPE_SEAWATER=2;
    private static final int DRINK_COUNT = 6;
    private static final int DRINK_ADD = 3000;

    public Kettle() {
        super(new Properties().durability(DRINK_COUNT).defaultDurability(DRINK_COUNT).tab(ItemGroups.Life).setNoRepair().food(new Food.Builder().nutrition(0).saturationMod(0).alwaysEat().build()));
    }

    @Override
    public boolean canAttackBlock(BlockState p_195938_1_, World p_195938_2_, BlockPos p_195938_3_, PlayerEntity p_195938_4_) {
        return false;
    }

    @Override
    public boolean isEnchantable(ItemStack p_77616_1_) {
        return false;
    }

    @Override
    public ActionResult<ItemStack> use(World world, PlayerEntity playerEntity, Hand hand) {
        ItemStack itemstack = playerEntity.getItemInHand(hand);
        RayTraceResult raytraceresult = getPlayerPOVHitResult(world, playerEntity,  RayTraceContext.FluidMode.SOURCE_ONLY);
        BlockRayTraceResult blockraytraceresult = (BlockRayTraceResult)raytraceresult;
        if (raytraceresult.getType() == RayTraceResult.Type.BLOCK) {
            BlockPos blockpos = blockraytraceresult.getBlockPos();
            Direction direction = blockraytraceresult.getDirection();
            BlockPos blockpos1 = blockpos.relative(direction);
            if (world.mayInteract(playerEntity,blockpos) && playerEntity.mayUseItemAt(blockpos1,direction,itemstack)) {
                if (world.getBlockState(blockpos).getBlock().getRegistryName().getPath().toLowerCase().contains("water")) {

                    CompoundNBT compoundNBT = itemstack.getTag();
                    if (world.getBiomeManager().getBiome(blockpos).getBiomeCategory().equals(Biome.Category.OCEAN)) {
                        compoundNBT.putInt(WATER_TYPE, WATERTYPE_SEAWATER);
                    } else {
                        compoundNBT.putInt(WATER_TYPE, WATERTYPE_FRESHWATER);
                    }
                    itemstack.setTag(compoundNBT);
                    itemstack.getItem().setDamage(itemstack, 0);
                    return ActionResult.fail(itemstack);
                }
            }
        }
        if (this.getDamage(itemstack)>=DRINK_COUNT) {
            CompoundNBT compoundNBT = itemstack.getTag();
            compoundNBT.putInt(WATER_TYPE,WATERTYPE_NONE);
            itemstack.setTag(compoundNBT);
            return ActionResult.fail(itemstack);
        }
        DrinkHelper.useDrink(world,playerEntity,hand);
        return ActionResult.pass(itemstack);
    }

    @Override
    public ItemStack finishUsingItem(ItemStack itemStack, World world, LivingEntity livingEntity) {
        if (this.getDamage(itemStack) < DRINK_COUNT && livingEntity instanceof ServerPlayerEntity) {
            ServerPlayerEntity player = (ServerPlayerEntity) livingEntity;

            CompoundNBT compoundNBT = itemStack.getTag();
            int i = compoundNBT.getInt(WATER_TYPE);
            if (i == WATERTYPE_FRESHWATER && world instanceof ServerWorld) {
                PlayerDataManager.setServerPlayerData(player, PlayerDataType.WATER, PlayerDataManager.getInt(player, PlayerDataType.WATER) + DRINK_ADD);
            } else if (i == WATERTYPE_SEAWATER && world instanceof ServerWorld) {
                livingEntity.addEffect(new EffectInstance(Effects.thirsty, 5000, 0));
                PlayerDataManager.setServerPlayerData(player, PlayerDataType.WATER, PlayerDataManager.getInt(player, PlayerDataType.WATER) + DRINK_ADD);
            }
            itemStack.setTag(compoundNBT);
            this.setDamage(itemStack, this.getDamage(itemStack) + 1);
        }
        return itemStack;
    }

    @Override
    public boolean doesSneakBypassUse(ItemStack stack, IWorldReader world, BlockPos pos, PlayerEntity player) {
        return true;
    }

    @Override
    public UseAction getUseAnimation(ItemStack p_77661_1_) {
        return UseAction.DRINK;
    }

    @Override
    public boolean isEdible() {
        return true;
    }

    @Override
    public int getUseDuration(ItemStack p_77626_1_) {
        return 32;
    }


}
